//
//  GroupGoodsDetailCtr-ext.swift
//  eemall
//
//  Created by zhuxietong on 2019/1/9.
//  Copyright © 2019 tianrun. All rights reserved.
//

import Foundation
import soapp
import AsyncDisplayKit
import Eelay
extension GroupGoodsDetailCtr{
    func reloadGroups(info:NSMutableDictionary) {
        
        let groups = DataNode.path(node: "list", info, value: NSMutableArray())
        let oldGroups = DataNode.path(node: "section1", tableNode.model, value: NSMutableArray())
        if oldGroups.count > 0
        {
            if groups.count < 1{
                tableNode.model <-- ("section0.1.__show","NO",NodeInsert.dictValue)
                tableNode.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .none)
            }
        }
        if oldGroups.count < 1
        {
            if groups.count > 0{
                tableNode.model <-- ("section0.1.__show","YES",NodeInsert.dictValue)
                tableNode.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .none)
            }
        }
        
        
        if oldGroups.count != groups.count{
            
            tableNode.model["section1"] = groups
            tableNode.reloadSections(IndexSet(integer: 1), with: .left)
            inRefreshGroup = false
            return

        }
        
        var animateIndexs = [IndexPath]()
        var currentIndexs = [IndexPath]()
        groups.list { (obj:NSMutableDictionary, i) in
            var needRefreshAnimation = true
            if oldGroups.count > i
            {
                if let old = oldGroups.object(at: i) as? NSMutableDictionary{
                    if obj["teamid",""] == old["teamid",""]{
                        needRefreshAnimation = false
                        if obj["residualnum",""] != old["residualnum",""]{
                        
                            let num = obj[int:"residualnum",0] - old[int:"residualnum",0]
                            if num > 0{
                                obj["__changed"] = num
                            }
                        }
                    }
                }
            }
            if needRefreshAnimation{
                animateIndexs.append(IndexPath(row: i, section: 1))
            }
            else{
                currentIndexs.append(IndexPath(row: i, section: 1))
            }
        }
        
        
        self.tableNode.model["section1"] = groups
        tableNode.reloadRows(at: currentIndexs, with: .none)
        tableNode.reloadRows(at: animateIndexs, with: .left)
        weak var wself = self
        co_delay(delay: 0.3) {
            wself?.inRefreshGroup = false
        }
    }
    
    @objc func requuestGroupData() {
        if inRefreshGroup{
            return
        }
        inRefreshGroup = true
        weak var wself = self
        //        loadingV.loading()
        JoTask().get.params(["goodsid":goodsID,"page":1,"pagesize":2])._surl(.正在拼团列表).json_handle { (status, msg, obj, resp) in
            if status.isSuccess{
                if let dict = obj as? NSMutableDictionary{
                    wself?.reloadGroups(info: dict)
                    return
                }
            }
            }.run()
    }
    
    
    func startTimer(interval:Int = 10) {
        weak var wself = self
        timer?.suspend()
        self.timer = SwiftTimer(interval: .seconds(interval), repeats: true, queue: .main, handler: { (timer) in
            wself?.requuestGroupData()
        })
        timer?.start()
    }
    
    func stopTimer() {
        timer?.suspend()
    }
}
